using System;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using System.Diagnostics;
using DevExpress.Xpo.DB;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                UseDataStore();
            }
            else
            {
                UseCachedDataStore();
            }
        }
        private static void UseCachedDataStore()
        {
            CachedDataStoreContractClient proxy = new CachedDataStoreContractClient();
            CachedDataStoreClientProxy remoteDataStore = new CachedDataStoreClientProxy(proxy);

            // Get the reference to the remote data store
            //Binding binding = new NetTcpBinding(SecurityMode.None);
            //ChannelFactory<Common.IDataStoreContract> factory = new ChannelFactory<Common.IDataStoreContract>(binding, "net.tcp://localhost:7465/XPOService");
            //         DataStoreClientProxy remoteDataStore = new DataStoreClientProxy(factory.CreateChannel( ));

            DataCacheNode cacheNode = new DataCacheNode(remoteDataStore);

            // Initialize XPO to use that reference
            XpoDefault.DataLayer = new SimpleDataLayer(cacheNode);

            Console.WriteLine("XPO configured for remote data store");

            // Do something XPO-ish to test things
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                TestClass testClass = new TestClass(unitOfWork);
                testClass.TestString = "Test string";
                TestClass2 testClass2 = new TestClass2(unitOfWork);
                testClass2.TestString2 = "Test string2";
                unitOfWork.CommitChanges();
            }

            Console.WriteLine("Test data created");

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                TestClass testClass = unitOfWork.FindObject<TestClass>(new BinaryOperator("TestString", "Test string"));
                Debug.Assert(testClass != null);
            }

            Console.WriteLine("Test data checked");


            // Be nice and close the connection to the remote store
            //((IClientChannel) remoteDataStore.WrappedDataStoreContract).Close( );
            proxy.Close();

            Console.WriteLine("Connection closed, hit Return to quit");

            Console.ReadLine();
        }
        private static void UseDataStore()
        {
            DataStoreContractClient proxy = new DataStoreContractClient();
            DataStoreClientProxy remoteDataStore = new DataStoreClientProxy(proxy);

            // Get the reference to the remote data store
            //Binding binding = new NetTcpBinding(SecurityMode.None);
            //ChannelFactory<Common.IDataStoreContract> factory = new ChannelFactory<Common.IDataStoreContract>(binding, "net.tcp://localhost:7465/XPOService");
            //         DataStoreClientProxy remoteDataStore = new DataStoreClientProxy(factory.CreateChannel( ));

            // Initialize XPO to use that reference
            XpoDefault.DataLayer = new SimpleDataLayer(remoteDataStore);

            Console.WriteLine("XPO configured for remote data store");

            // Do something XPO-ish to test things
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                TestClass testClass = new TestClass(unitOfWork);
                testClass.TestString = "Test string";
                TestClass2 testClass2 = new TestClass2(unitOfWork);
                testClass2.TestString2 = "Test string2";
                unitOfWork.CommitChanges();
            }

            Console.WriteLine("Test data created");

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                TestClass testClass = unitOfWork.FindObject<TestClass>(new BinaryOperator("TestString", "Test string"));
                Debug.Assert(testClass != null);
            }

            Console.WriteLine("Test data checked");


            // Be nice and close the connection to the remote store
            //((IClientChannel) remoteDataStore.WrappedDataStoreContract).Close( );
            proxy.Close();

            Console.WriteLine("Connection closed, hit Return to quit");

            Console.ReadLine();
        }
    }

    public class TestClass : XPObject
    {
        public TestClass()
        {

        }

        public TestClass(Session session)
            : base(session)
        {

        }

        private string testString;
        public string TestString
        {
            get
            {
                return testString;
            }
            set
            {
                testString = value;
            }
        }
    }

    public class TestClass2 : XPObject
    {
        public TestClass2()
        {

        }

        public TestClass2(Session session)
            : base(session)
        {

        }

        private string testString;
        public string TestString2
        {
            get
            {
                return testString;
            }
            set
            {
                this.SetPropertyValue<string>("TestString2", value);
            }
        }
    }
}